 # -*- Mode: Python -*- vi:si:et:sw=4:sts=4:ts=4:syntax=python
import sys
import shutil

from cerbero.tools.libtool import LibtoolLibrary

GRAPHENE_CONFIG_UNVERSAL='''\
#ifdef __i386__
#include "i386/graphene-config.h"
#elif defined(__ppc__)
#include "ppc/graphene-config.h"
#elif defined(__x86_64__)
#include "x86_64/graphene-config.h"
#elif defined(__arm__)
#include "arm/graphene-config.h"
#elif defined(__arm64__)
#include "arm64/graphene-config.h"
#else
#error "Unsupported Architecture"
#endif
'''

class Recipe(recipe.Recipe):
    name = 'graphene'
    stype = SourceType.TARBALL
    btype = BuildType.MESON
    version = '1.8.2'
    url = 'https://github.com/ebassi/graphene/archive/{0}.tar.gz'.format(version)
    tarball_checksum = '8c7d895536936e65c7f859f87bd14c258ac7f2c4ec999010e39e658f47c4d2ee'
    licenses = [{License.MIT: ['LICENSE']}]
    # Disable NEON completely, it's broken and experimental
    meson_options = {'arm_neon' : 'false', 'introspection' : 'false' }

               # https://github.com/ebassi/graphene/pull/127
    patches = ['graphene/0001-meson-fix-HAVE_INIT_ONCE-declaration.patch',
               # https://github.com/ebassi/graphene/pull/128
               'graphene/0001-Fix-implicit-conversion-error-when-compiling-with-MS.patch',
               # https://github.com/ebassi/graphene/pull/130
               'graphene/0001-bench-Fix-MSVC-build-error-on-32-bit-Windows.patch',
               # https://github.com/ebassi/graphene/pull/173
               'graphene/0001-meson-Don-t-error-with-MSVC-C4819-warning.patch']

    deps = ['glib']

    files_libs = ['libgraphene-1.0']
#    files_typelibs = ['Graphene-1.0']

    files_devel = [
        'include/graphene-1.0',
        'lib/graphene-1.0/include',
        'lib/pkgconfig/graphene-1.0.pc',
        'lib/pkgconfig/graphene-gobject-1.0.pc',
    ]

    def prepare(self):
        if self.config.variants.gi:
            self.deps.append("gobject-introspection")
            self.meson_options['instrospection'] = 'true'

        # Graphene don't support SSE2 dynamically, disable for now
        if self.config.target_arch == Architecture.X86:
            self.meson_options['sse2'] = 'false'
            self.meson_options['gcc_vector'] = 'false'

        # -msse4.1 causes clang to segfault building graphene-matrix.c on OS X
        # when building for x64_64. Disable it for now.
        if self.config.target_platform in [Platform.DARWIN, Platform.IOS]:
            arch = self.config.target_arch
            if arch == Architecture.X86_64:
                self.set_env('SSE41_CFLAGS', ' ')

    def post_install(self):
        if self.config.target_platform in [Platform.DARWIN, Platform.IOS]:
            # For the universal build we need to ship graphene-config.h of all
            # architectures in a subfolder and include the correct one depending
            # on the compiler architecture
            arch = self.config.target_arch
            if arch == Architecture.X86:
                arch = 'i386'
            elif arch == Architecture.ARM64:
                arch = 'arm64'
            elif Architecture.is_arm(arch):
                arch = 'arm'

            arch_dir = os.path.join(self.config.prefix, 'lib', 'graphene-1.0',
                                     'include', arch)
            if not os.path.exists(arch_dir):
                os.makedirs(arch_dir)
            shutil.copyfile(os.path.join(self.meson_dir, 'src', 'graphene-config.h'),
                        os.path.join(arch_dir, 'graphene-config.h'))
            with open(os.path.join(self.config.prefix, 'lib', 'graphene-1.0',
                                   'include', 'graphene-config.h'), 'w+') as f:
                f.write(GRAPHENE_CONFIG_UNVERSAL)

        lib = LibtoolLibrary('graphene-1.0', None, None, None, self.config.libdir,
                self.config.target_platform, deps=['gobject-2.0', '-lm', '-pthread'])
        lib.save()
        super().post_install()
